import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import updateButtonStatus from './updateButtonStatus';
import {toast} from 'nc-lightapp-front';

export default function saveOpinionData(callback) {
    const optionData = this.props.editTable.getAllRows('contopiniongrid', true);

    const validateCallback = () => {
        proFetch({
            url: '/nccloud/hrcm/contopinion/OpinionRemcSaveAction.do',
            body: {
                contopiniongrid: {
                    rows: optionData
                }
            }
        }).then((res) => {
            if (res.success) {
                toast({color: "success", content: this.state.json['cm6011-000005']});
                this.props.editTable.setStatus('contopiniongrid', 'browse');
                if (res.data && res.data.contopiniongrid) {
                    this.props.editTable.setTableData('contopiniongrid', {rows: res.data.contopiniongrid.rows});
                } else {
                    this.props.editTable.setTableData('contopiniongrid', {rows: []});
                }
                this.setState({
                    isEdit: false
                });
                updateButtonStatus.call(this, this.state.orgVal);
                callback && callback();
            }
        });
    };

    const cardData = {
        pageid: this.state.pageid,
        gridModel: {
            areacode: 'contopiniongrid',
            pageinfo: {},
            rows: optionData
        }
    };
    this.props.validateToSave(cardData, validateCallback, {contopiniongrid: 'editTable'}, 'grid');
}
